(ns redrouter.macros
  (:require
   [camel-snake-kebab.core :as csk]
   [clojure.string :refer [split]]
   [redrouter.react-router :refer [react-router-components react-router-hooks]]))

(defn get-module-path [module-name]
  (split module-name #"\."))

(defn def-component
  "声明组件"
  [ns component]
  (let [component-name (csk/->kebab-case-symbol (name component))
        module-path (get-module-path (name component))]
    `(def ~component-name
       (utils/->reagent-component ~ns ~module-path))))

(defn def-fun
  "声明函数"
  [ns fun]
  (let [fun-name (csk/->kebab-case-symbol (name fun))
        module-path (get-module-path (name fun))]
    `(defn ~fun-name [& args#]
       (apply utils/call-func ~ns ~module-path args#))))


(defmacro export-react-router-components
  "定义react-router组件"
  []
  `(do ~@(map (fn [c] (def-component 'rd c)) react-router-components)))

(defmacro export-react-router-hooks
  "定义react-router Hooks"
  []
  `(do ~@(map (fn [hook] (def-fun 'rd hook)) react-router-hooks)))
